using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using EvoWars.Stuffs;
using EvoWars.Stuffs.TankStuff;

namespace EvoWars2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont MyFont;
        GameController controller;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            controller = new GameController();

            graphics.PreferredBackBufferWidth = controller.Camera.Width;
            graphics.PreferredBackBufferHeight = controller.Camera.Height;



        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            controller.Initialize();



        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            controller.TheWorld.Texture = CreateRectangleTexture(controller.TheWorld.Width, controller.TheWorld.Height, Color.Cornsilk);
            foreach (Tank player in controller.TheWorld.Players)
            {
                player.Texture = Content.Load<Texture2D>("tank2");
            }

            MyFont = Content.Load<SpriteFont>("DefaultFont");

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {

            controller.Update(Keyboard.GetState(), gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            //draw the world texture
            spriteBatch.Draw(controller.TheWorld.Texture, controller.Camera.GetRenderPosition(new Vector2(0f, 0f)), Color.White);



            //draw the players on top of the world
            foreach (Tank player in controller.TheWorld.Players)
            {
                Color playerColor = Color.Red;
                if (player.IsHumanControlled)
                {
                    playerColor = Color.White;
                }

                Vector2 originVector = new Vector2(player.Texture.Width / 2f, player.Texture.Height / 2f);


                spriteBatch.Draw(player.Texture, controller.Camera.GetRenderPosition(player.Position), null, playerColor, player.Heading, originVector, 1f, SpriteEffects.None, 1);
                spriteBatch.DrawString(MyFont, player.Name, controller.Camera.GetRenderPosition(player.Position), Color.SaddleBrown);
            }

            spriteBatch.End();


            base.Draw(gameTime);
        }



        private Texture2D CreateRectangleTexture(int width, int height, Color fillColor)
        {
            // create the rectangle texture, ,but it will have no color! lets fix that
            Texture2D rectangleTexture = new Texture2D(GraphicsDevice, width, height, 1, TextureUsage.None, SurfaceFormat.Color);

            //set the color to the amount of pixels in the textures
            Color[] color = new Color[width * height];

            //loop through all the colors setting them to whatever values we want
            for (int i = 0; i < color.Length; i++)
            {
                color[i] = fillColor;
            }

            //set the color data on the texture
            rectangleTexture.SetData(color);

            //return the texture
            return rectangleTexture;
        }
    }
}
